package com.wanban.downloader.utils

import com.blankj.utilcode.util.ThreadUtils

object MyThreadUtils {

    fun <T> executeSingleTask(async: () -> T){
        executeSingleTask(async, {}, {})
    }

    fun <T> executeSingleTask(async: () -> T, success: (t: T) -> Unit){
        executeSingleTask(async, success) {}
    }


    fun <T> executeSingleTask(async: () -> T, success: (t: T) -> Unit, error: (t: Throwable?) -> Unit){
        ThreadUtils.executeBySingle(object: ThreadUtils.Task<T>(){
            override fun doInBackground(): T {
                return  async()
            }

            override fun onCancel() {

            }

            override fun onFail(t: Throwable?) {
                error(t)
            }

            override fun onSuccess(result: T) {
                success(result)
            }
        })
    }
}